
import java.util.Arrays;

/**
 * Creat with IntelliJ IDEA
 * Description
 * User: mamba24
 * Date: 2022-04-24
 * Time: 23:21
 * To BE A Better Person-2020-3-2
 */
class MyArraryList{
    public int[] val;
    public int UsedSize;

    public MyArraryList() {
        this.val=new int[5];
        UsedSize=0;
    }
    // 打印顺序表
    public void display() {
        for (int i = 0; i < UsedSize; i++) {
            System.out.print(this.val[i]+" ");
        }
    }
    // 新增元素,默认在数组最后新增
    public void add(int data) {
        if (isFull() == false) {
            val[UsedSize]=data;
        }else{
            addSpace();
            val[UsedSize]=data;
        }
        UsedSize++;
    }
    // 在 pos 位置新增元素
    public void add(int pos, int data) {
        if(pos>this.val.length){
            System.out.println("error");
        }else{
            if (isFull() == false) {
                //后移
                for (int i = this.UsedSize; i > pos-1 ; i--) {
                    this.val[i]=this.val[i-1];
                }
                this.val[pos-1]=data;
            }else{
                addSpace();
                for (int i = this.UsedSize; i > pos-1 ; i--) {
                    this.val[i]=this.val[i-1];
                }
                this.val[pos-1]=data;
            }
            UsedSize++;
        }

    }
    // 判定是否包含某个元素
    public boolean contains(int toFind) {
        for (int i = 0; i < this.UsedSize; i++) {
            if(this.val[i]==toFind){
                return true;
            }
        }
        return false;
    }
    // 查找某个元素对应的位置
    public int indexOf(int toFind) {
        for (int i = 0; i < this.UsedSize; i++) {
            if(this.val[i]==toFind){
                return i+1;
            }
        }
        return -1;
    }
    // 获取 pos 位置的元素
    public int get(int pos) {
        if(pos<=UsedSize){
            return this.val[pos];
        }
        return -1;
    }
    // 给 pos 位置的元素设为 value
    public void set(int pos, int value) {
        if(pos>this.val.length){
            System.out.println("error");
        }else{
            this.val[pos-1]=value;
        }


    }
    //删除第一次出现的关键字key
    public void remove(int toRemove) {
        //删除数字
        int pos=0;
        for (int i = 0; i < this.UsedSize; i++) {
            if(this.val[i]==toRemove){
                this.val[i]=0;
                pos=i;
                break;
            }
        }
        /**
         * 20220426写到这里卡住了
         *jjjjj
         */
        //移动数字
        for(int i=pos+1;i<UsedSize-1;i++){
            this.val[i]=this.val[i+1];
        }
        this.UsedSize--;
    }
    // 获取顺序表长度
    public int size() {
        return this.UsedSize;
    }
    // 清空顺序表
    public void clear() {
        for(int i=0;i<this.UsedSize;i++){
            this.val[i]=0;
        }
    }
    //扩容
    public void addSpace(){

        this.val= Arrays.copyOf(val,(val.length)*2);
    }
    //判断是否满了
    public boolean isFull(){

        return UsedSize==this.val.length;
    }
}
public class TestDemo {
    public static void main(String[] args) {
        MyArraryList myArraryList =new MyArraryList();
        myArraryList.add(1);
        myArraryList.add(2);
        myArraryList.add(3);
        myArraryList.add(4);
        myArraryList.add(5);
        myArraryList.add(6);
        myArraryList.add(3,7);
        System.out.println(myArraryList.contains(7));
        myArraryList.display();
        myArraryList.remove(7);
        myArraryList.display();
    }
}
